package com.youlu.campus.website.service.impl;

import com.youlu.campus.website.entity.CompanyDynamicDetail;
import com.youlu.campus.website.entity.CompanyDynamicType;
import com.youlu.campus.website.entity.CompanyDynamicTypePageQuery;
import com.youlu.campus.website.repositories.CompanyDynamicTypeRepository;
import com.youlu.campus.website.service.CompanyDynamicTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 动态类型服务实现类
 */
@Service
public class CompanyDynamicTypeServiceimpl implements CompanyDynamicTypeService {

    @Autowired
    private CompanyDynamicTypeRepository typeRepository;

    @Resource(name = "websiteMongoTemplate")
    private MongoTemplate mongoTemplate;


    @Override
    public CompanyDynamicType save(CompanyDynamicType type) {
        if (StringUtils.isBlank(type.getId())) {
            type.setId(null);
            type.setCreateTime(LocalDateTime.now());
        } else {
            type.setUpdateTime(LocalDateTime.now());
        }
        return typeRepository.save(type);
    }

    @Override
    public CompanyDynamicType findById(String id) {
        return typeRepository.findById(id).orElse(null);
    }

    @Override
    public List<CompanyDynamicType> getAllTypes() {
        return typeRepository.findAll(Sort.by("createTime").descending());
    }

    @Override
    public Page<CompanyDynamicType> findByPage(CompanyDynamicTypePageQuery pageQuery) {
        Query query = new Query();
        if (StringUtils.isNotBlank(pageQuery.getName())) {
            query.addCriteria(Criteria.where("name").regex(pageQuery.getName(), "i"));
        }
        //统计总条数（分页需要先查总数）
        long total = mongoTemplate.count(query, CompanyDynamicType.class);

        Pageable pageRequest = PageRequest.of(pageQuery.getPage() - 1, pageQuery.getPageSize(), Sort.by("createTime").descending());
        List<CompanyDynamicType> list;
        if (total == 0) {
            list = Arrays.asList();
        } else {
            // 应用分页（设置跳过的条数和查询的条数）
            query.skip(pageRequest.getOffset()).limit(pageRequest.getPageSize());
            // 应用排序
            query.with(pageRequest.getSort());
            list = mongoTemplate.find(query, CompanyDynamicType.class);
        }
        return PageableExecutionUtils.getPage(list, pageRequest, () -> total);
    }

    @Override
    public void delete(String id) {
        typeRepository.deleteById(id);
    }

    @Override
    public List<CompanyDynamicType> findByTypeIn(List<String> typeList) {
        return typeRepository.findByTypeIn(typeList);
    }
}